package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *工单
 *
 *@author weijianxun
 */
@Service("workOrderDynamicStrategyImpl")
public class WorkOrderDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(WorkOrderDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(WorkOrderEnum.LINK_CUSTOMER.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
        refTextToLogTypeMap.put(WorkOrderEnum.LINK_OPPORTUNITY.getAttr(), ProjectLogEnum.OPPORTUNITY_REF.getSubType());
        refTextToLogTypeMap.put(WorkOrderEnum.LINK_CONTRACT.getAttr(), ProjectLogEnum.CONTRACT_REF.getSubType());
        refTextToLogTypeMap.put(WorkOrderEnum.LINK_CONTACT.getAttr(), ProjectLogEnum.CONTACT_REF.getSubType());
    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
       try{
           SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
           WorkOrderEntity workOrderEntity = saasFormSaveDTO.getNewWorkOrderEntity();
           Long workOrderId = workOrderEntity.getId();
           // 52455 【动态】客户新建工单，动态显示工单编号为未知对象类型 workOrderEntity.getBusinessType()之前是能获取到的
           saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, null,
                   DynamicRefInfoDTO.initDynamicRefInfoDTO(workOrderId, XbbRefTypeEnum.WORK_ORDER.getCode(), workOrderEntity.getSerialNo()), ProjectLogEnum.WORK_ORDER_CREATE.getSubType());
       }catch (Exception e){
           LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.WorkOrderDynamicStrategyImpl.save", e);
       }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, null, WorkOrderEnum.SHEET_NO.getAttr(), ProjectLogEnum.WORK_ORDER_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.WorkOrderDynamicStrategyImpl.batchDelete", e);
        }
    }
}
